
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author BENSON
 */
public class ControllerModifyEmp {
    Connection conn = null;
    String querry;
    Statement st = null;
    public ModifyEmployee modEmp;

    private ActionListener actionListener;

    public ControllerModifyEmp() {
        // this.deleteOrder = delete;
        modEmp = new ModifyEmployee();
        modEmp.setVisible(true);

    }

    public void setList() {
        String ky = "";
        ky = modEmp.getKey().getText();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull", "root", "root");
            querry = "update flights set fName =?,ffrom=?,destination=?,Price=?,fCapacity=?,depatureTime=?,arrivalTime=?"
                    + "where fName like '%" + ky + "%'";
            PreparedStatement stmt = conn.prepareStatement(querry);
        //st.execute("USE jetwingdb;");

            stmt.setString(1, modEmp.getFname().getText());
            stmt.setString(2, modEmp.getFromtext().getText());
            stmt.setString(3, modEmp.getDesttext().getText());
            stmt.setString(4, modEmp.getCosttext().getText());
            stmt.setString(5, modEmp.getCaptext().getText());
            stmt.execute();
   //stmt.close();
            conn.close();
            JOptionPane.showMessageDialog(null, "Flight modified succesfully");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }

    public void listFlights() {

        String ky = "";
        ky = modEmp.getKey().getText();
        System.out.println(ky);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull", "root", "root");
            querry = "SELECT * FROM Flights where fName like '%" + ky + "%'";
            st = conn.prepareStatement(querry);
            st.execute("USE jetwingdb;");

            ResultSet rs = st.executeQuery(querry);
            while (rs.next()) {

                modEmp.getFname().setText(rs.getString("fName"));
                modEmp.getFromtext().setText(rs.getString("ffrom"));
                modEmp.getDesttext().setText(rs.getString("destination"));
                modEmp.getCosttext().setText(rs.getString("Price"));
                modEmp.getCaptext().setText(rs.getString("fCapacity"));
                modEmp.getDepttext().setText(rs.getString("depatureTime"));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }

    public void contol() {
        actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                if (actionEvent.getSource() == modEmp.getSaveChanges()) {
                    setList();
                }
                if (actionEvent.getSource() == modEmp.getClose()) {
                    modEmp.setVisible(false);

                }
                if (actionEvent.getSource() == modEmp.getSearchButton()) {
                    listFlights();
                }
            }

        };
        modEmp.getSearchButton().addActionListener(actionListener);
        modEmp.getClose().addActionListener(actionListener);
        modEmp.getSaveChanges().addActionListener(actionListener);
    }

    public static void main(String[] args) {
        ControllerModifyEmp c = new ControllerModifyEmp();
        c.contol();

    }
    
}
